//
// Created by Thomas on 20.04.2024.
//

#ifndef ARTILLERYSYSTEM_IDENTIFIABLELIST_H
#define ARTILLERYSYSTEM_IDENTIFIABLELIST_H

#include <vector>
#include <type_traits>
#include "Identifiable.h"

template<class T>
class IdentifiableList {
    static_assert(std::is_base_of<Identifiable, T>::value, "T must be a base of Identifiable");
    std::vector<T *> list;
public:
    void add(T *entity);

    T *removeById(std::string id);

    T *getById(std::string id);

    std::vector<T *> getAll();
};

template<class T>

std::vector<T *> IdentifiableList<T>::getAll() {
    return list;
}

template<class T>

T *IdentifiableList<T>::getById(std::string id) {

    for (T *&el: list) {
        if (el->getId() == id) return el;
    }

    return nullptr;
}

template<class T>

void IdentifiableList<T>::add(T *
entity) {
    list.push_back(entity);
}

template<class T>

T *IdentifiableList<T>::removeById(
        std::string id) {
    for (auto iter = list.begin(); iter != list.end(); iter++) {
        if ((*iter)->getId() == id) {
            auto found = *iter;
            list.erase(iter);
            return found;
        }
    }
    return nullptr;
}


#endif //ARTILLERYSYSTEM_IDENTIFIABLELIST_H
